package com.zl.gmadmin.controller;

import cn.hutool.json.JSONUtil;
import com.zl.gmadmin.common.ApiResponse;
import com.zl.gmadmin.entity.SysMenu;
import com.zl.gmadmin.exception.BaseException;
import com.zl.gmadmin.payload.MenuQueryCriteria;
import com.zl.gmadmin.service.SysMenuService;
import com.zl.gmadmin.utils.LogUtil;
import com.zl.gmadmin.utils.PageUtils;
import com.zl.gmadmin.utils.SecurityUtils;
import com.zl.gmadmin.vo.menu.MenuVo;
import com.zl.gmadmin.vo.menu.SysMenuVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

/**
 * @author zhangliang
 * @version 1.0
 * @date 2021/2/7 17:02
 */
@Api(tags = "系统:菜单管理")
@RestController
@RequestMapping("/api/menus")
public class SysMenuController {

    private final SysMenuService sysMenuService;

    public SysMenuController(SysMenuService sysMenuService) {
        this.sysMenuService = sysMenuService;
    }

    @GetMapping("/build")
    @ApiOperation("获取前端菜单")
    public ApiResponse buildMenus() {
        List<MenuVo> menuVos = sysMenuService.findMenuByUserId(SecurityUtils.getCurrentUserId());
        return ApiResponse.ofSuccess(menuVos);
    }

    @GetMapping
    @ApiOperation("查询菜单")
    public ApiResponse findMenus() {
        List<SysMenuVo> menus = sysMenuService.queryMenu();
        return ApiResponse.ofSuccess(PageUtils.toPage(menus, menus.size()));
    }


    @GetMapping("supMenus")
    @ApiOperation("查询当前节点的下级所有子节点")
    public ApiResponse findSupMenus(MenuQueryCriteria criteria) {
        List<SysMenuVo> menus = sysMenuService.querySupMenu(criteria.getPid());
        return ApiResponse.ofSuccess(menus);
    }

    @GetMapping("maxSort")
    @ApiOperation("最大排序值")
    public ApiResponse findMaxMenuSort() {
        Long menuSort = sysMenuService.queryMenuSort("max");
        return ApiResponse.ofSuccess(menuSort);
    }

    @PostMapping
    @ApiOperation("新增菜单")
    public ApiResponse addMenu(@RequestBody SysMenu sysMenu) {
        if (Objects.isNull(sysMenu.getTitle())){
            throw  new BaseException(400,"错误");
        }
        sysMenuService.addMenu(sysMenu);
        return ApiResponse.ofSuccess();


    }

    @DeleteMapping
    @ApiOperation("删除菜单")
    public ApiResponse del(@RequestBody List<Long> ids) {
        sysMenuService.removeByIds(ids);
        return ApiResponse.ofSuccess();


    }

    @PutMapping
    @ApiOperation("更新菜单信息")
    public ApiResponse updateMenu(@RequestBody SysMenu sysMenu) {
        sysMenuService.updateById(sysMenu);
        return ApiResponse.ofSuccess();
    }

    @ApiOperation("导出用户信息")
    @GetMapping("/download")
    public void download(MenuQueryCriteria criteria, HttpServletResponse response) throws IOException {

        LogUtil.debug("导出:" + JSONUtil.toJsonStr(criteria));
        sysMenuService.download(response, criteria.getIds());
    }
}
